package com.shuke.medical.insure.billDownload;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.URLUtil;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/***
 * @title RemoteFileUlr
 * @description
 * @author woo hua
 * @version 1.0.0
 * @create 2023/4/13 15:33
 **/
public class RemoteFileUlr {

    public static void main(String[] args) throws IOException {
/*        //读取服务器上的文件内容
        String urlPath="http://img.elongstatic.com/mas/podDescription/input.json";
        // 统一资源
        URL url = new URL(urlPath);
        // 连接类的父类，抽象类
        URLConnection urlConnection = url.openConnection();
        // http的连接类
        HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
        //设置超时
        httpURLConnection.setConnectTimeout(1000*5);
        //设置请求方式，默认是GET
//        httpURLConnection.setRequestMethod("POST");
        // 设置字符编码
        httpURLConnection.setRequestProperty("Charset", "UTF-8");
        // 打开到此 URL引用的资源的通信链接（如果尚未建立这样的连接）。
        httpURLConnection.connect();
        // 文件大小
        int fileLength = httpURLConnection.getContentLength();

        // 控制台打印文件大小
        System.out.println("您要下载的文件大小为:" + fileLength / (1024 * 1024) + "MB");

        // 建立链接从请求中获取数据
        URLConnection con = url.openConnection();
        */
        URL url1 = URLUtil.toUrlForHttp("http://img.elongstatic.com/mas/podDescription/input.json");
        InputStream stream = URLUtil.getStream(url1);
        BufferedInputStream bufferedInput = new BufferedInputStream(stream);
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        StringBuilder bu=new StringBuilder();
        while ((bytesRead = bufferedInput.read(buffer)) != -1) {
            String chunk = new String(buffer, 0, bytesRead);
            bu.append(chunk);

        }
        System.out.print("======================" + bu);



    }
}
